//package cz.semjobKB.xml;
//
//import java.io.File;
//import java.util.ArrayList;
//import java.util.List;
//
//import javax.xml.parsers.DocumentBuilder;
//import javax.xml.parsers.DocumentBuilderFactory;
//import javax.xml.parsers.ParserConfigurationException;
//import javax.xml.transform.OutputKeys;
//import javax.xml.transform.Transformer;
//import javax.xml.transform.TransformerException;
//import javax.xml.transform.TransformerFactory;
//import javax.xml.transform.dom.DOMSource;
//import javax.xml.transform.stream.StreamResult;
//
//import org.w3c.dom.Document;
//import org.w3c.dom.Element;
//import cz.semjobKB.conllx.api.ISentence;
//import cz.semjobKB.conllx.api.IWord;
//import cz.semjobKB.triples.api.IMatch;
//import cz.semjobKB.triples.api.impl.TripleMatch;
//
//public class CreateXMLOutput {
//	public void writeXML (File sourceFile, List<ISentence> sentences, List<TripleMatch> matches) throws Exception {
//		try {
//			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
//			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//			docBuilder = docFactory.newDocumentBuilder();
//
//
//			// root elements
//			Document doc = docBuilder.newDocument();
//			Element rootElement = doc.createElement("document");
//			rootElement.setAttribute("name", sourceFile.getName().replace(".conll", ""));
//			doc.appendChild(rootElement);
//			Integer count = 0;
//			int predId = 0;
//			for (TripleMatch match : matches) {
//				Element sent = doc.createElement("sentence");
//				sent.setAttribute("id", count.toString());
//
//				List<IWord> words = new ArrayList<IWord>(match.getSentence().getWords());
//				List<Integer> pred_matches = new ArrayList<Integer>();
//
//				if (match.getPredicateMatch() != null && match.getPredicateMatch().size() > 0) {
//					for (IMatch prMatch : match.getPredicateMatch()) {
//						pred_matches.add(prMatch.getPositionRootMatch());
//						words.get(words.indexOf(prMatch.getMatchWord())).setText("///PREDICATE1///"+ count + "_" + predId + "///" + prMatch.getMatch() + "///");						
//						predId++;
//					}
//				}
//
//				if (match.getObjectMatch() != null && match.getObjectMatch().size() > 0) {
//					int objId = 0;
//					for (IMatch obMatch : match.getObjectMatch()) {
//						if (pred_matches.indexOf(obMatch.getPositionRootMatch()) == -1) {
//							throw new Exception("Root Position wasnt found!!!!!");
//						}
//						int predIdRoot = predId - (pred_matches.indexOf(obMatch.getPositionRootMatch()) + 1); 
//
//						words.get(words.indexOf(obMatch.getMatchWord())).setText("///OBJECT2///"+ count + "_" + predIdRoot + "_" + objId +  "///" + obMatch.getMatch() + "///");
//						objId++;
//					}
//				}
//
//				if (match.getSubjectMatch() != null && match.getSubjectMatch().size() > 0) {
//					int subId = 0;
//					for (IMatch suMatch : match.getSubjectMatch()) {
//
//						if (pred_matches.indexOf(suMatch.getPositionRootMatch()) == -1) {
//							throw new Exception("Root Position wasnt found!!!!!");
//						}
//
//						int predIdRoot = predId - (pred_matches.indexOf(suMatch.getPositionRootMatch()) + 1);
//						words.get(words.indexOf(suMatch.getMatchWord())).setText("///SUBJECT3///" + count + "_" + predIdRoot + "_" + subId + "///" + suMatch.getMatch()+ "///");
//						subId++;
//					}
//				}
//				
//				String sentence = getText(words);
//				sentence = sentence.trim();
//
//				String[] parts = sentence.split("///");
//				if (parts.length > 1) {
//					for (int pos = 0 ; pos < parts.length; pos++) {
//						if (parts[pos].isEmpty()) {
//							continue;
//						}
//						if (parts[pos].equals("PREDICATE1")) {
//							Element pred = doc.createElement("predicate");
//							pred.setAttribute("id", parts[pos + 1]);
//							pred.appendChild(doc.createTextNode(parts[pos + 2]));
//							sent.appendChild(pred);
//							pos = pos + 2;
//							continue;
//						}
//						if (parts[pos].equals("OBJECT2")) {
//							Element obj = doc.createElement("object");
//							obj.setAttribute("id", parts[pos + 1]);
//							obj.appendChild(doc.createTextNode(parts[pos + 2]));
//							sent.appendChild(obj);
//							pos = pos + 2;
//							continue;
//						}
//						if (parts[pos].equals("SUBJECT3")) {
//							Element sub = doc.createElement("subject");
//							sub.setAttribute("id", parts[pos + 1]);
//							sub.appendChild(doc.createTextNode(parts[pos + 2]));
//							sent.appendChild(sub);
//							pos = pos + 2;
//							continue;
//						}
//						sent.appendChild(doc.createTextNode(parts[pos]));
//					}
//				} else {
//					sent.appendChild(doc.createTextNode(sentence));
//				}
//
//
//				rootElement.appendChild(sent);
//				count++;
//			}
//
//			// write the content into xml file
//			TransformerFactory transformerFactory = TransformerFactory.newInstance();
//			Transformer transformer = transformerFactory.newTransformer();
//			DOMSource source = new DOMSource(doc);
//			StreamResult result = new StreamResult(new File(sourceFile.getAbsolutePath().replace("txt.conll", "xml")));
//
//			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
//			transformer.transform(source, result);
//		} catch (ParserConfigurationException pce) {
//			pce.printStackTrace();
//		} catch (TransformerException tfe) {
//			tfe.printStackTrace();
//		}
//
//	
//	}
//	
//	private  String getText(List<IWord> words) {
//		//if (text == null) {
//		//	return root.getText().substring(startIndex - root.getStartIndex(), endIndex - root.getStartIndex());
//		//}
//		String text = "";		
//		for (IWord word : words) {
//			text += word.getText() + " ";
//		}		
//		return text;
//	}
//}
